﻿/******************************************************************************************************
Copyright (c) 2014 Steven Giacomelli (stevegiacomelli@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************************************/

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace SharpGeo
{
    /// <summary>
    /// An lightweight immutable class to store geocentric coordinates.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    [DebuggerDisplay("({X},{Y},{Z})")]
    sealed public class GeocentricCoordinate :
        IEquatable<IGeocentricCoordinate>,
        IGeocentricCoordinate
    {
        #region Constructors

        /// <summary>
        /// Constructs a new GeocentricCoordinate
        /// </summary>
        /// <param name="x">The X value</param>
        /// <param name="y">The Y value</param>
        /// <param name="z">The Z value</param>
        public GeocentricCoordinate(double x, double y, double z)
        {
            _x = x;
            _y = y;
            _z = z;
        }

        /// <summary>
        /// Creates a clone of a IGeocentricCoordinate
        /// </summary>
        /// <param name="coordinate">The IGeocentricCoordinate to clone</param>
        public GeocentricCoordinate(IGeocentricCoordinate coordinate)
        {
            if (coordinate == null)
                throw new ArgumentNullException("coordinate",
                    Properties.Resources.GeocentricCoordinateGeocentricCoordinateNullArgumentException);

            _x = coordinate.X;
            _y = coordinate.Y;
            _z = coordinate.Z;
        }

        #endregion Constructors

        #region Private Fields

        private double _x;
        private double _y;
        private double _z;

        #endregion Private Fields

        #region Properties

        /// <summary>
        /// Gets the X value
        /// </summary>
        public double X { get { return _x; } }

        /// <summary>
        /// Gets the Y value
        /// </summary>
        public double Y { get { return _y; } }

        /// <summary>
        /// Gets the Z value
        /// </summary>
        public double Z { get { return _z; } }

        /// <summary>
        /// Gets the oordinate at the specified axis
        /// </summary>
        /// <param name="axis">The axis of the oordinate to retrieve</param>
        /// <returns>Returns the oordiante at the specified axis, if the axis not between 0 and 2, double.NaN is returned.</returns>
        public double this[int axis]
        {
            get
            {
                if (axis == 0)
                {
                    return _x;
                }
                else if (axis == 1)
                {
                    return _y;
                }
                else if (axis == 2)
                {
                    return _z;
                }
                else
                {
                    return double.NaN;
                }
            }
        }

        /// <summary>
        /// Gets the number of Axes (2)
        /// </summary>
        int ICoordinate.NumberOfAxes { get { return 3; } }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Tests for equality with the passed obj
        /// </summary>
        /// <param name="obj">The passed obj to test for equality with</param>
        /// <returns>Returns true if the passed obj equals the value of this GeocentricCoordinate</returns>
        public override bool Equals(object obj)
        {
            var coordinate = obj as IGeocentricCoordinate;

            if (obj == null || coordinate == null)
                return false;

            return coordinate.X == X && coordinate.Y == Y && coordinate.Z == Z;
        }

        /// <summary>
        /// Tests for equality with the passed IGeocentricCoordinate
        /// </summary>
        /// <param name="other">The passed IGeocentricCoordinate to test for equality with</param>
        /// <returns>Returns true if the passed obj equals the value of this GeocentricCoordinate</returns>
        public bool Equals(IGeocentricCoordinate other)
        {
            if (other == null)
            {
                return false;
            }

            return other.X == X && other.Y == Y && other.Z == Z;
        }

        /// <summary>
        /// Converts this GeocentricCoordinate to a string
        /// </summary>
        /// <returns>A string representing this GeocentricCoordinate</returns>
        public override string ToString()
        {
            return string.Format(System.Globalization.CultureInfo.InvariantCulture, "({0},{1},{2})", X, Y, Z);
        }

        /// <summary>
        /// Returns a hashcode representing this GeocentricCoordinate
        /// </summary>
        /// <returns>The computed hashcode representing this GeocentricCoordinate</returns>
        public override int GetHashCode()
        {
            unchecked
            {
                long l = BitConverter.DoubleToInt64Bits(_z);
                l *= l * 31 ^ BitConverter.DoubleToInt64Bits(_y);
                l *= l * 37 ^ BitConverter.DoubleToInt64Bits(_x);

                return (int)unchecked((l >> 32) ^ l);
            }
        }

        #endregion Methods
    }
}